﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Core;
using Microsoft.Reporting.WebForms;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Business.Report;

namespace iOak.Web.Report
{
    public partial class ApplyReports : System.Web.UI.Page
    {
        private static readonly PolicyService itemPolicyService = new PolicyService();
        private static readonly ResumeTemplateService itemResumeTemplateService = new ResumeTemplateService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly CompanyService itemCompanyService = new CompanyService();
        private static readonly CampaignService itemCampaignService = new CampaignService();
        private static readonly SummeryReportService itemSummeryReportService = new SummeryReportService();
        private static readonly CollegeReportService itemCollegeReportService = new CollegeReportService();
        private WorkAssistant _workassistant = new WorkAssistant();
        protected void Page_Load(object sender, EventArgs e)
        {
            Guid campaignId = new Guid(Request.QueryString["CampaignId"]);

            var itemCampaign = itemCampaignService.Retrieve(campaignId);
            var itemPolicy = itemPolicyService.Retrieve(itemCampaign.PolicyId);
            var itemTelplate = itemResumeTemplateService.Retrieve(itemPolicy.ResumeTemplateId);

            dynamic listDetial = new List<dynamic>();
            string SumBounty = "0";
            if (itemTelplate.SummaryReportPath.Equals(@"Report\CollegeReport.rdlc"))
            {
                listDetial = itemCollegeReportService.GetDetialList(campaignId, _workassistant.CurrentUser.CompanyId);
                SumBounty = Math.Round(itemCollegeReportService.GetSumBounty(listDetial, itemCampaign.Bounty), 2).ToString();
            }
            else if (itemTelplate.SummaryReportPath.Equals(@"Report\ApplyReport.rdlc"))
            {
                listDetial = itemSummeryReportService.GetDetialList(campaignId, _workassistant.CurrentUser.CompanyId);
                SumBounty = Math.Round(itemCampaign.Bounty * listDetial.Count, 2).ToString();
            }

            // 报表路径
            ReportViewer1.LocalReport.ReportPath = itemTelplate.ApplyReportPath;

            var itemApp = itemApplicationService.GetListByCampaignIdAndCompanyId(campaignId, _workassistant.CurrentUser.CompanyId);
            if (itemApp == null)
            {
                Response.Write("<scripwindow.opener=true;window.close();</script>");
            }
            var item = itemCompanyService.Retrieve(itemApp.CompanyId);
            ReportParameter paramReportHead = new ReportParameter("ReportParameter1", item.CompanyName);
            ReportParameter paramSumBounty = new ReportParameter("paramSumBounty", SumBounty);
            ReportParameter paramBankName = new ReportParameter("BankName", item.BankName);
            ReportParameter paramBankAccountNo = new ReportParameter("BankAccountNo", item.BankAccountNo);
            ReportParameter paramSumNumber = new ReportParameter("SumNumber", listDetial.Count.ToString());
            ReportParameter paramUserName = new ReportParameter("UserName", _workassistant.CurrentUser.FullName);
            ReportParameter paramContactPhone = new ReportParameter("ContactPhone", item.ContactPhone);
            ReportParameter paramAssuredLaborCode = new ReportParameter("AssuredLaborCode", item.AssuredLaborCode);
            ReportParameter[] parms = new ReportParameter[] { paramReportHead, paramSumBounty, paramBankName, paramBankAccountNo, 
                paramSumNumber,paramUserName,paramContactPhone,paramAssuredLaborCode };

            ReportViewer1.LocalReport.SetParameters(parms);
            ReportViewer1.LocalReport.Refresh();
            byte[] bytes = CommonHelper.ReportToByte(ReportViewer1);

            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("补贴申请表.pdf", System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
        }
    }
}